package com.mybatisplus.mp.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常拦截
 */
@RestControllerAdvice
@Slf4j
public class GlobalThrow {
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object HandleException(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        Map<String, String> map = new HashMap<>();
        bindingResult.getFieldErrors().forEach(error->{
            map.put(error.getField(), error.getDefaultMessage());
        });
        return map;
    }


    @ExceptionHandler(Throwable.class)
    public Object getException(Throwable throwable) {
        log.error("message:",throwable);
        return throwable;

    }
}
